package com.cg.controller.business;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cg.controller.AbstractController;
import com.cg.entity.BaseBusiness;
import com.cg.entity.BaseComment;
import com.cg.entity.BusinessDoc;
import com.cg.entity.SysUserEntity;
import com.cg.service.impl.BaseBusinessServiceImpl;
import com.cg.service.impl.BusinessSearchServiceImpl;
import com.cg.utils.Constant;
import com.cg.utils.DateUtils;
import com.cg.utils.Query;
import com.cg.utils.R;
import com.cg.utils.annotation.SysLog;
import com.cg.utils.validator.ValidatorUtils;
import com.cg.utils.validator.group.AddGroup;

/**
 * 基础布局
 * @author cumt
 *
 */
@RestController
@RequestMapping("/sys/basebusiness")
public class BaseBusinessController extends AbstractController{

	@Autowired
	BaseBusinessServiceImpl baseBusinessServiceImpl;
	
	@Autowired
	BusinessSearchServiceImpl businessSearchServiceImpl;
	/**
	 * 所有列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:basebusiness:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}
		//查询列表数据
		Query query = new Query(params);
//		BaseBusiness business=new BaseBusiness();
		List<BaseBusiness> businessList = baseBusinessServiceImpl.queryList(query);
		int total = baseBusinessServiceImpl.queryTotal(query);
		
		//PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());
		
		return R.success(total, "", businessList);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:basebusiness:info")
	public R info(@PathVariable("id") Long id){
		BaseBusiness business = baseBusinessServiceImpl.queryObject(id);
		
		
		return R.ok().put("business", business);
	}
	
	
	/**
	 * 业务列表
	 */
	@RequestMapping("/select")
	@RequiresPermissions("sys:basebusiness:select")
	public R select(String type){
//		BusinessDoc doc=new BusinessDoc();
		
		List list = null;
		Map<String, Object> doc = new HashMap<>();
		if("2".equals(type)) {
			list = businessSearchServiceImpl.queryListByGroup(doc);
		}
		else {
			list = baseBusinessServiceImpl.queryList(doc );
		}
		 
		
		return R.ok().put("list", list);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存")
	@RequestMapping("/save")
	@RequiresPermissions("sys:basebusiness:save")
	public R save(@RequestBody BaseBusiness business){
		business.setCreateTime(DateUtils.dateToDateString(new Date()));
		baseBusinessServiceImpl.save(business);
		
		return R.ok();
	}
	/**
	 * 修改用户
	 */
	@SysLog("修改")
	@RequestMapping("/update")
	@RequiresPermissions("sys:basebusiness:update")
	public R update(@RequestBody BaseBusiness business){
		
//		comment.set(getUserId());
		baseBusinessServiceImpl.update(business);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:basebusiness:delete")
	public R delete(@RequestBody Long[] ids){
		
		baseBusinessServiceImpl.deleteBatch(ids);
		
		return R.ok();
	}
	
	
	
}
